package com.epoint.mobileoa.ui.todolist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.GetArchiveListTask;
import com.epoint.mobileoa.model.ArchiveList;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.IOHelp;
import com.epoint.mobileoa.tools.ToastHelp;

public class TodolistView extends SuperView{

	public static final int GetArchiveList = 1;

	public static int allArchiveNums;

	private ListView lv;

	private int shownum = 15;// 每页显示数量

	private int pagenum = 1;// 第几页

	private LinearLayout foot;

	List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();

	private int visibleItemCount;// 一页可显示的List数量

	@Override
	public void initControls() {
		lv = (ListView) findViewById(R.id.lv_mailstyle);
		lv.setOnItemClickListener(this);
		addFoot();
	}

	public void addFoot() {
		TextView load_bt = new TextView(this);
		load_bt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void getData(int pagenum, int shownum) {
		top_pb.setVisibility(View.VISIBLE);
		HashMap param = new HashMap();
		param.put("pagenum", pagenum);
		param.put("shownum", shownum);
		Task task = new GetArchiveListTask(this, param);
		MainService.addTask(task);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void refresh(Object... param) {
		if (param[0].toString().equals(MainService.NORMAL)) {
			List<ArchiveList> alist = (List<ArchiveList>) param[1];
			for (int i = 0; i < alist.size(); i++) {
				Map<String, Object> listItem = new HashMap<String, Object>();

				String title = alist.get(i).title;

				String type = "待办";
				if (alist.get(i).archivetype.equals("fawen")) {
					type = "发文";
				} else if (alist.get(i).archivetype.equals("shouwen")) {
					type = "收文";
				} else if (alist.get(i).archivetype.equals("baozhengjin")) {
					type = "保证金";
				}
				listItem.put("messageitemguid", alist.get(i).messageitemguid);
				listItem.put("archiveguid", alist.get(i).archiveguid);
				listItem.put("typecode", alist.get(i).archivetype);
				listItem.put("dqbuzhou", alist.get(i).beizhu);
				listItem.put("tjr", alist.get(i).tijiaoren);
				listItem.put("type", type);
				listItem.put("title", title);
				listItem.put("beizhu", "步骤：" + alist.get(i).beizhu);
				listItem.put("generatedate", "发送日期："
						+ alist.get(i).generatedate);
				listItem.put("tijiaoren", "提交人：" + alist.get(i).tijiaoren);
				listItem.put("banliren", "办理人：" + alist.get(i).banliren);
				listItems.add(listItem);
			}
			SimpleAdapter sa = new SimpleAdapter(this, listItems,
					R.layout.achive_lv_style,
					new String[] { "type", "title", "banliren", "tijiaoren",
					"beizhu", "generatedate" }, new int[] {
					R.id.achive_lv_type, R.id.achive_lv_title,
					R.id.achive_lv_blr, R.id.achive_lv_tjr,
					R.id.achive_lv_bzmc, R.id.achive_lv_fssj });
			lv.setAdapter(sa);
			if (pagenum > 1) {
				lv.setSelection(sa.getCount() - shownum - visibleItemCount + 1);
			}
			if (sa.getCount() == allArchiveNums) {
				lv.removeFooterView(foot);
			}
		}
		top_pb.setVisibility(View.INVISIBLE);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		if (position == listItems.size()) {
			pagenum++;
			getData(pagenum, shownum);
			return;
		}

		if (listItems.get(position).get("typecode").equals("")) {
			ToastHelp.showToastCommon(this, "该条待办请在PC端处理！");
		} else if (listItems.get(position).get("typecode").equals("shouwen")) {
			Intent it_sw = new Intent(this, ShouWenView.class);
			it_sw.putExtra("messageitemguid",
					listItems.get(position).get("messageitemguid").toString());
			it_sw.putExtra("archiveguid",
					listItems.get(position).get("archiveguid").toString());
			it_sw.putExtra("typecode", listItems.get(position).get("typecode")
					.toString());
			it_sw.putExtra("dqbuzhou", listItems.get(position).get("dqbuzhou")
					.toString());
			it_sw.putExtra("tjr", listItems.get(position).get("tjr").toString());
			startActivity(it_sw);
			finish();
		} else if (listItems.get(position).get("typecode").equals("fawen")) {
			Intent it_fw = new Intent(this, FaWenView.class);
			it_fw.putExtra("messageitemguid",
					listItems.get(position).get("messageitemguid").toString());
			it_fw.putExtra("archiveguid",
					listItems.get(position).get("archiveguid").toString());
			it_fw.putExtra("typecode", listItems.get(position).get("typecode")
					.toString());
			it_fw.putExtra("dqbuzhou", listItems.get(position).get("dqbuzhou")
					.toString());
			it_fw.putExtra("tjr", listItems.get(position).get("tjr").toString());
			startActivity(it_fw);
			finish();
		} 
		else if (listItems.get(position).get("typecode").equals("demo")){
			IOHelp.doOpenFile(this, "/sdcard/mobileoa/attachdownload/demo/fwd.doc");
		}
		else if (listItems.get(position).get("typecode").equals("demo2")){
			Intent aIntent = new Intent(Intent.ACTION_VIEW);
			aIntent.setClassName("com.lanting.pdfdemo", "cn.fingersoft.moa.demo.MainActivity");
			startActivity(aIntent); 
		}
		/*else if (listItems.get(position).get("typecode")
				.equals("baozhengjin")) {
			Intent intent = new Intent(this, BaozhengjinDetailView.class);
			intent.putExtra("messageitemguid",
					listItems.get(position).get("messageitemguid").toString());
			intent.putExtra("archiveguid",
					listItems.get(position).get("archiveguid").toString());
			intent.putExtra("typecode", listItems.get(position).get("typecode")
					.toString());
			intent.putExtra("dqbuzhou", listItems.get(position).get("dqbuzhou")
					.toString());
			intent.putExtra("tjr", listItems.get(position).get("tjr")
					.toString());
			startActivity(intent);
			finish();
		}*/
	}

	@Override
	public int getLayoutInt() {
		return R.layout.layout_mailstyle;
	}

//	@Override
//	public String setTitle() {
//		return getString(R.string.dbsy);
//	}


	/* (non-Javadoc)
	 * @see com.epoint.mobileoa.bizlogic.SuperView#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		/*listItems.clear();
		Map<String, Object> listItem = new HashMap<String, Object>();
		listItem.put("typecode", "demo");
		listItem.put("type", "发文");
		listItem.put("title", "【批阅】发文V1.0");
		listItem.put("banliren", "办理人：李铭远");
		listItem.put("tijiaoren", "提交人：李铭远");
		listItem.put("beizhu", "步骤：领导批阅");
		listItem.put("generatedate", "发送日期：2012-12-10 10:01");
		listItems.add(listItem);

		listItem = new HashMap<String, Object>();
		listItem.put("typecode", "demo2");
		listItem.put("type", "发文");
		listItem.put("title", "【加盖印章】发文V1.0");
		listItem.put("banliren", "办理人：张维新");
		listItem.put("tijiaoren", "提交人：张维新");
		listItem.put("beizhu", "步骤：加盖印章");
		listItem.put("generatedate", "发送日期：2012-12-11 14:23");
		listItems.add(listItem);*/
		listItems.clear();
		getData(pagenum, shownum);
	
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

	@Override
	public String setTitle() {
		// TODO Auto-generated method stub
		return "待办事宜";
	}
	
}
